﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Media;
using ThunderboltInterfaceSharp.ViewModels.UserControls.Download;

namespace ThunderboltInterfaceSharp.Convert
{
    /// <summary>
    /// Bool值装字符串
    /// </summary>
    public class BoolToColorString : IValueConverter
    {
		//todo:界面上定义控件不触发
		//todo:代码定义的控件触发一次就不会再次触发了
		//todo:不使用转换器可以正常触发背景色
		//原因：分清楚是Binding还是TemplateBinding，Binding是绑ViewModel，TemplateBinding是绑自己
		//结合两个问题，猜测是自定义控件的问题不是界面的问题
		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string de = "Transparent";
            //var dBrush = Brushes.Transparent;
			if ((bool)value)
            {
                de = "#2c3138";
                //dBrush = Brushes.SlateGray;
            }
			//return dBrush;

			return de;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return "Transparent";
        }
    }



    public class ContextMenuAddOffset : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (double)value+5.00;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class ContextMenuSubtractOffset : IValueConverter
    {
	    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
	    {
		    return (double)value - 5.00;
	    }
	    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
	    {
		    throw new NotImplementedException();
	    }
    }
    public class ObjectToPath : IValueConverter
    {
	    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
	    {
		    return (Geometry)value;
	    }
	    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
	    {
		    throw new NotImplementedException();
	    }
    }


    /// <summary>
    /// Bool转Visibility
    /// </summary>
    public class BoolToVisibility : IValueConverter
    {
	    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
	    {
		    var v = Visibility.Collapsed; ;
		    if ((bool)value)
		    {
			    v = Visibility.Visible;
		    }
		    return v;
	    }
	    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
	    {
		    return false;
	    }
    }



	/// <summary>
	/// DownloadStatus转文字描述
	/// </summary>
	public class DownloadStatusToText : IValueConverter
    {
	    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
	    {
		    return value switch
		    {
			    DownloadStatus.Downloading => "下载中",
			    DownloadStatus.Paused => "已暂停",
			    DownloadStatus.Completed => "已完成",
			    DownloadStatus.DownloadFailed => "下载失败",
			    _ => "未知"
		    };
	    }
	    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
	    {
		    return DownloadStatus.DownloadFailed;
	    }
    }
	/// <summary>
	/// 位置转边距
	/// </summary>
	public class PositionToMargin : IValueConverter
	{
		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			return value switch
			{
				PlacementMode.Top => new Thickness(0, -5, 0, 0),
				PlacementMode.Bottom => new Thickness(0, 0, 0, -5),
				PlacementMode.Left => new Thickness(-5, 0, 0, 0),
				PlacementMode.Right => new Thickness(0, 0, -5, 0),
				_ => "未知"
			};
		}
		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			return PlacementMode.Top;
		}
	}

	/// <summary>
	/// bool值转前景色
	/// </summary>
	public class BoolToForeground : IValueConverter
	{
		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			var ret = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#5C5C5C"));


			if (value != null && (bool)value)
			{
				ret = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#E6E6E6"));
			}
			return ret;
		}

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			throw new NotImplementedException();
		}
	}
}